package com.devicemantra.coordinate.jaxb;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

import com.devicemantra.coordinate.exceptions.jaxb.*;





public class JAXBContextFactory {
	
	private static JAXBContext mJAXBContextInstance =null;
	
	/** Since JAXBContext is thread safe we create just one instance of it 
	 *  - Amith 17th March, 2010 - 4 months - Kukku */
	public static JAXBContext getInstance() throws CJAXBException {
		
		if(null == mJAXBContextInstance)
		{
			createInstance();
		}
		return mJAXBContextInstance;
	}
	
	private static void createInstance() throws CJAXBException {
		try {
			if( mJAXBContextInstance == null )
				mJAXBContextInstance = JAXBContext.newInstance( "com.devicemantra.coordinate.jaxb" );
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			mJAXBContextInstance = null;
			throw new CJAXBException("ERROR: Could not create JAXB Context ");
		}
		
	}
	
	/** Since Marshaller is *NOT* thread safe we create one instance per request 
	 * 	of it - Amith 17th March, 2010 - 4 months - Kukku 
	 * @throws CJAXBContextCreationException */
	public static Marshaller createMarshaller() throws CJAXBException
	{
		Marshaller lMarshaller = null;
		if(null == mJAXBContextInstance)
		{
			createInstance();
		}
		try {
			lMarshaller = mJAXBContextInstance.createMarshaller();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new CJAXBException("ERROR: Creating Marshaller");
		}
		return lMarshaller;
		
	}
	
	/** Since UnMarshaller is *NOT* thread safe we create one instance per request 
	 * 	of it - Amith 17th March, 2010 - 4 months - Kukku 
	 * @throws CJAXBContextCreationException */
	public static Unmarshaller createUnMarshaller() throws CJAXBException
	{
		Unmarshaller lUnMarshaller = null;
	/*	if(mJAXBContextInstance == null)
			System.out.println("mJAXBContextInstance value is  null");
		else
			System.out.println("mJAXBContextInstance value is NOT null"); */
		
		if(null == mJAXBContextInstance)
		{
			createInstance();
		}
		
		/*if(mJAXBContextInstance == null)
			System.out.println("mJAXBContextInstance value is  null");
		else
			System.out.println("mJAXBContextInstance value is NOT null"); */
		
		try {
			lUnMarshaller = mJAXBContextInstance.createUnmarshaller();
			SchemaFactory sf = SchemaFactory.newInstance(
				      javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
			Schema schema = null;
			try {
				schema = sf.newSchema(new File("coordinate.xsd"));
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			lUnMarshaller.setSchema(schema);
			lUnMarshaller.setEventHandler(new UnMarshalEventHandler());
		} catch(JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new CJAXBException("ERROR: Could not create Unmarshaller");
		}
		return lUnMarshaller;
		
	}
	
}
